@using OrchardCore.Autoroute.ViewModels;
@using OrchardCore.ContentManagement;
@using OrchardCore.ContentManagement.Routing;
@model AutoroutePartViewModel
@inject IAuthorizationService AuthorizationService
@inject OrchardCore.Settings.ISiteService SiteService
@inject OrchardCore.ContentManagement.IContentManager ContentManager
@{
    var site = await SiteService.GetSiteSettingsAsync();
    var containedContentItemsAspect = await ContentManager.PopulateAspectAsync<ContainedContentItemsAspect>(Model.ContentItem);
}

@if (Model.Settings.AllowDisabled)
{
    <div class="form-group">
        <div class="custom-control custom-checkbox">
            <input type="checkbox" class="custom-control-input" asp-for="Disabled" checked="@Model.Disabled" />
            <label class="custom-control-label" asp-for="Disabled">@T["Disabled"]</label>
            <span class="hint">— @T["Check to disable autoroute for this content item."]</span>
        </div>
    </div>
}

<div class="autoroute-disabled" style="@(Model.Disabled ? "display: none" : "")">
    <div class="form-group" asp-validation-class-for="Path">
        <label asp-for="Path">@T["Permalink"]</label>
        <div class="input-group">
            @if (!String.IsNullOrWhiteSpace(site.BaseUrl))
            {
                <div class="input-group-prepend">
                    <div class="input-group-text">@site.BaseUrl</div>
                </div>
            }
            <input asp-for="Path" class="form-control" disabled="@(!Model.Settings.AllowCustomPath)" />
            <span asp-validation-for="Path"></span>
        </div>
        @if (!String.IsNullOrWhiteSpace(Model.Settings.Pattern) && Model.Settings.AllowCustomPath)
        {
            <span class="hint">@T["The url of the content item. Leave empty to auto-generate it."]</span>
        }
        else
        {
            <span class="hint">@T["The url of the content item. It will be automatically generated."]</span>
        }
    </div>
    @{
        var authorized = await AuthorizationService.AuthorizeAsync(User, Permissions.SetHomepage);
        var showHomepageOption = Model.Settings.ShowHomepageOption && authorized;

        if (Model.IsHomepage)
        {
            <div class="alert alert-info">@T["This content item is the current homepage"]</div>
        }
        else
        {
            if (showHomepageOption)
            {
                <div class="form-group">
                    <div class="custom-control custom-checkbox">
                        <input type="checkbox" class="custom-control-input" asp-for="SetHomepage" checked="@Model.SetHomepage" />
                        <label class="custom-control-label" asp-for="SetHomepage">@T["Set as homepage"]</label>
                        <span class="hint">— @T["Check to set this content item as the homepage once published."]</span>
                    </div>
                </div>
            }
        }
    }

    @if (Model.Settings.AllowUpdatePath)
    {
        <div class="form-group">
            <div class="custom-control custom-checkbox">
                <input type="checkbox" class="custom-control-input" asp-for="UpdatePath" checked="@Model.UpdatePath" />
                <label class="custom-control-label" asp-for="UpdatePath">@T["Refresh the path"]</label>
                <span class="hint">— @T["Check to refresh the path. Warning: the previous URL won't be accessible anymore."]</span>
            </div>
        </div>
    }

    @if (containedContentItemsAspect.Accessors.Any() && Model.Settings.AllowRouteContainedItems)
    {
        <div class="form-group">
            <div class="custom-control custom-checkbox">
                <input type="checkbox" class="custom-control-input" asp-for="RouteContainedItems" checked="@Model.RouteContainedItems" />
                <label class="custom-control-label" asp-for="RouteContainedItems">@T["Route contained items"]</label>
                <span class="hint">— @T["Check to enabling the routing of child content items."]</span>
            </div>
        </div>
    }

    @if (Model.Settings.ManageContainedItemRoutes && Model.Settings.AllowAbsolutePath)
    {
        <div class="form-group">
            <div class="custom-control custom-checkbox">
                <input type="checkbox" class="custom-control-input" asp-for="Absolute" checked="@Model.Absolute" />
                <label class="custom-control-label" asp-for="Absolute">@T["Absolute"]</label>
                <span class="hint">— @T["Check to specify that a child content item will be routed to an absolute path."]</span>
            </div>
        </div>
    }
</div>

@if (Model.Settings.AllowDisabled)
{
    <script at="Foot" depends-on="jquery">
        //<![CDATA[
        $('#@Html.IdFor(i => i.Disabled)').change(function (e) {
            var checked = $(e.target).prop('checked');
            $('.autoroute-disabled').each(function () {
                checked ? $(this).hide() : $(this).show();
            });
        });
        //]]>
    </script>
}
